package com.baidu.eureka.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author caixiaofeng
 * @version 1.0
 * @Description: <<自动填充bean>>
 * @company 浙江优创信息技术有限公司
 * @create 2020-07-18 15:42
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入时填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //实体类中有该属性时调用
        boolean flae = metaObject.hasSetter("createTime");
        if(flae){
            this.fillStrategy(metaObject,"createTime", LocalDateTime.now());
        }
    }

    /**
     * 更新时填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        //更新对象时未设置值时填充
        Object val = this.getFieldValByName("createTime", metaObject);
        if(val==null){
            this.fillStrategy(metaObject,"updateTime", LocalDateTime.now());
        }

    }
}
